package cn.wonderland.user.service.impl;

import cn.wonderland.basic.cons.EmailRegistCon;
import cn.wonderland.basic.cons.PhoneRegistCon;
import cn.wonderland.basic.util.AjaxResult;
import cn.wonderland.basic.util.BussinessException;
import cn.wonderland.basic.util.MD5Utils;
import cn.wonderland.user.domain.Logininfo;
import cn.wonderland.user.domain.User;
import cn.wonderland.user.dto.AccountLoginDto;
import cn.wonderland.user.dto.EmailRegisterDto;
import cn.wonderland.user.dto.PhoneRegisterDto;
import cn.wonderland.user.mapper.LogininfoMapper;
import cn.wonderland.user.mapper.UserMapper;
import cn.wonderland.user.service.IUserService;
import cn.wonderland.basic.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wonderland
 * @since 2023-04-30
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    /**
     * 邮箱注册
     *
     * @param dto
     */
    @Override
    public void emailRegister(EmailRegisterDto dto) {
        // 非空校验
        // 验证两次密码是否正确
        if (!dto.getPassword().equals(dto.getPasswordRepeat())) {
            throw new BussinessException("两次密码不一致，请重新输入！");
        }
        // 验证短信验证码是否过期、是否正确
        String redisEmailCode = (String) redisTemplate.opsForValue()
                .get(String.format(EmailRegistCon.REDIS_KEY, dto.getEmail()));
        if (StringUtils.isBlank(redisEmailCode) || !redisEmailCode.equalsIgnoreCase(dto.getEmailCodeValue())) {
            throw new BussinessException("邮箱验证码错误！");
        }
        // 校验手机号是否注册过
        // 判断邮箱号是否注册过 去user表中找是否存在
        User user = userMapper.findByEmail(dto.getEmail());
        if (user != null) {
            throw new BussinessException("该邮箱号已经注册过，请去登录！");
        }
        // 保存数据 先保存loginingo再保存user
        user = dto.getUser();
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setDisable(true);
        logininfo.setType(1);
        logininfoMapper.add(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.add(user);
    }


    /**
     * 手机号注册
     *
     * @param dto
     */
    @Override
    public void phoneRegister(PhoneRegisterDto dto) {
        // 非空校验
        // 验证两次密码是否正确
        if (!dto.getPassword().equals(dto.getPasswordRepeat())) {
            throw new BussinessException("两次密码不一致，请重新输入！");
        }
        // 验证短信验证码是否过期、是否正确
        String redisSmsCode = (String) redisTemplate.opsForValue()
                .get(String.format(PhoneRegistCon.REDIS_KEY, dto.getPhone()));
        if (StringUtils.isBlank(redisSmsCode) || !redisSmsCode.equalsIgnoreCase(dto.getSmsCodeValue())) {
            throw new BussinessException("短信验证码错误！");
        }
        // 校验手机号是否注册过
        User user = userMapper.findByPhone(dto.getPhone());
        if (user != null) {
            throw new BussinessException("该手机号已经注册过，请去登录！");
        }
        // 保存数据 先保存loginingo再保存user
        user = dto.getUser();
        Logininfo logininfo = new Logininfo(); // 创建一个空的logininfo对象
        BeanUtils.copyProperties(user, logininfo); // 将user对象的字段值复制给logininfo
        logininfo.setDisable(true); // 设置默认值
        logininfo.setType(1); // 设置默认值
        logininfoMapper.add(logininfo); // 先添加logininfo
        user.setLogininfoId(logininfo.getId()); // 获取logininfoId 赋值给user
        userMapper.add(user); // 保存user对象

    }


}
